package com.yljk.yljkproject.utils;

import android.app.ActivityManager;
import android.app.Application;
import android.content.pm.PackageManager;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMOptions;
import com.hyphenate.push.EMPushConfig;
import com.yilijk.base.utils.DevicesUtils;
import com.yljk.servicemanager.constants.ConfigApi;
import com.yljk.servicemanager.constants.SdkConfigApi;

import java.util.Iterator;
import java.util.List;

/**
 * Created by SHICHENG
 * <p>
 * Time on 2022/03/01
 */
public class ThreeSDKUtils {

    private static ThreeSDKUtils threeSDKUtils;

    public synchronized static ThreeSDKUtils getInstance(){
        if (threeSDKUtils == null){
            threeSDKUtils = new ThreeSDKUtils();
        }
        return threeSDKUtils;
    }

    public void IMInit(Application application) {

        EMOptions options = new EMOptions();
        if (ConfigApi.Open.isOpenUnlinePush) {
            EMPushConfig.Builder builder = new EMPushConfig.Builder(application);
            //判断手机
            if (RomUtil.isEmui()) {
                builder.enableHWPush();
            } else if (RomUtil.isMiui()) {
                builder.enableMiPush("2882303761519857784", "5321985739784");
            } else if (RomUtil.isVivo()) {
                builder.enableVivoPush();
            } else if (RomUtil.isOppo()) {
                builder.enableOppoPush("5675b5657ebf4105ab1a192bdf5f85bc", "9ab25a13df854a5bbf4a4ab1e7b8c815");
            } else if (RomUtil.isFlyme()) {

            } else if (RomUtil.is360()) {
            }
            options.setPushConfig(builder.build());
        }
        //       私有化设置
        if (ConfigApi.Open.isRestServer) {
            options.setRestServer(DevicesUtils.isEnvironment() ? SdkConfigApi.Im.test_RestServer : SdkConfigApi.Im.RestServer);//设置私有云rest地址（ip/域名：port）
            options.setIMServer(DevicesUtils.isEnvironment() ? SdkConfigApi.Im.test_Server : SdkConfigApi.Im.Server);//设置私有云IM地址
            options.setImPort(DevicesUtils.isEnvironment() ? SdkConfigApi.Im.test_Port : SdkConfigApi.Im.Port);//设置私有云IM端口号
            options.enableDNSConfig(SdkConfigApi.Im.enableDNSConfig);//默认是true，在私有云下，需要关闭（设置成false）
            options.setAppKey(DevicesUtils.isEnvironment() ? SdkConfigApi.Im.test_Key : SdkConfigApi.Im.key);
        } else {
            options.setAppKey(DevicesUtils.isEnvironment() ? SdkConfigApi.Im.test_Key : SdkConfigApi.Im.key);
        }

// 默认添加好友时，是不需要验证的，改成需要验证
        options.setAcceptInvitationAlways(false);
// 是否自动将消息附件上传到环信服务器，默认为True是使用环信服务器上传下载，如果设为 false，需要开发者自己处理附件消息的上传和下载
        options.setAutoTransferMessageAttachments(true);
// 是否自动下载附件类消息的缩略图等，默认为 true 这里和上边这个参数相关联
        options.setAutoDownloadThumbnail(true);
        //自动登录
        options.setAutoLogin(false);
        int pid = android.os.Process.myPid();
        String processAppName = getAppName(application, pid);
// 如果APP启用了远程的service，此application:onCreate会被调用2次
// 为了防止环信SDK被初始化2次，加此判断会保证SDK被初始化1次
// 默认的APP会在以包名为默认的process name下运行，如果查到的process name不是APP的process name就立即返回

        if (processAppName == null || !processAppName.equalsIgnoreCase(application.getPackageName())) {
//            Log.e(TAG, "enter the service process!");
            // 则此application::onCreate 是被service 调用的，直接返回
            return;
        }
//初始化
        EMClient.getInstance().init(application, options);
//在做打包混淆时，关闭debug模式，避免消耗不必要的资源
        EMClient.getInstance().setDebugMode(DevicesUtils.isEnvironment());
        EMClient.getInstance().getOptions().setRequireDeliveryAck(true);
        //IM环信推送
//        EMPushConfig.Builder builder = new EMPushConfig.Builder(context);
//        builder.enableVivoPush() // 推送证书相关信息配置在AndroidManifest.xml中
//                .enableMeiZuPush("", "")
//                .enableMiPush("", "")
//                .enableOppoPush("", "")
//                .enableHWPush() //开发者需要调用该方法来开启华为推送
//                .enableFCM(""); //开发者需要调用该方法来开启FCM推送
//
//        EMOptions options = new EMOptions();
//        options.setPushConfig(builder.build());

    }


    //IMChat
    private String getAppName(Application application, int pID) {
        String processName = null;
        ActivityManager am = (ActivityManager) application.getSystemService(application.ACTIVITY_SERVICE);
        List l = am.getRunningAppProcesses();
        Iterator i = l.iterator();
        PackageManager pm = application.getPackageManager();
        while (i.hasNext()) {
            ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i.next());
            try {
                if (info.pid == pID) {
                    processName = info.processName;
                    return processName;
                }
            } catch (Exception e) {
                // ALog.d("Process", "Error>> :"+ e.toString());
            }
        }
        return processName;
    }

}
